﻿using System;
using System.ComponentModel.Composition;
using System.Data.OracleClient;
using System.Text;
using ParaEditor.Infrastructure;
using Micropayment.Model;
using ParaEditor.SysMgr.Service.Entity;

namespace ParaEditor.SysMgr.Service.OperatorMgrService.Impl
{
    [Export(typeof(IOperatorMgrService))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class OperatorMgrServiceImpl : IOperatorMgrService
    {
        public OperatorInfos GetOperatorInfo()
        {
            OracleDataReader dataReader;
            OperatorInfos _operators = new OperatorInfos();
            try
            {
                string sqlText = "select a.operator_id OPERATOR_ID,a.operator_name OPERATOR_NAME,b.rule_name RULE_NAME,a.OPERATOR_PWD operator_pwd from operator a,rule b where a.rule_id = b.rule_id and a.rule_id <> 0";
                dataReader = DataBase.SqlReader(sqlText);
                while (dataReader.Read())
                {
                    OperatorInfo _operator = new OperatorInfo();
                    _operator.operator_id = dataReader.GetOracleString(0).ToString();
                    _operator.operator_name = dataReader.GetOracleString(1).ToString();
                    _operator.rule_name = dataReader.GetOracleString(2).ToString();
                    _operator.operator_pwd = dataReader.GetOracleString(3).ToString();
                    _operators.Add(_operator);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            catch (Exception ex)
            {
            }
            return _operators;
        }

        public Rules GetRules()
        {
            Rules rules = new Rules();
            OracleDataReader dataReader;
            try
            {
                string sqlText = "select RULE_ID,RULE_NAME from RULE where RULE_ID <> 0";
                dataReader = DataBase.SqlReader(sqlText);
                while (dataReader.Read())
                {
                    RULE rule = new RULE();
                    rule.RULE_ID = (int)dataReader.GetOracleNumber(0);
                    rule.RULE_NAME = dataReader.GetOracleString(1).ToString();
                    rules.Add(rule);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            catch (Exception ex)
            { 
            }
            return rules;
        }



        public bool DeleteOpratorByID(string opt_id)
        {
            bool flag = false;
            string sqlText = "delete from OPERATOR  where OPERATOR_ID ='" + opt_id + "'";
            try
            {
                DataBase.SqlCommand(sqlText);
                flag = true;
            }
            catch (Exception e)
            {
                flag = false;
            }
            return flag;
        }

        public bool UpdateOpratorPwdByID(string opt_id, string opt_pwd)
        {
            bool flag = false;
            string sqlText = "update OPERATOR SET OPERATOR_PWD = '" + opt_pwd + "' where OPERATOR_ID ='" + opt_id + "'";
            try
            {
                DataBase.SqlCommand(sqlText);
                flag = true;
            }
            catch (Exception e)
            {
                flag = false;
            }
            return flag;
        }

        public bool UpdateOpratorByID(string opt_id, string opt_name, int role)
        {
            bool flag = false;
            string sqlText = "update OPERATOR SET OPERATOR_NAME = '" + opt_name + "',RULE_ID = " + role + " where OPERATOR_ID ='" + opt_id + "'";
            try
            {
                DataBase.SqlCommand(sqlText);
                flag = true;
            }
            catch (Exception e)
            {
                flag = false;
            }
            return flag;
        }

        public bool CreateOperator(OPERATOR _operator)
        {
            bool flag = false;
            StringBuilder sb = new StringBuilder("insert into OPERATOR(OPERATOR_ID,OPERATOR_NAME,OPERATOR_PWD,RULE_ID) values (");
            try
            {
                sb.Append("'" + _operator.OPERATOR_ID + "',");
                sb.Append("'" + _operator.OPERATOR_NAME + "',");
                sb.Append("'" + _operator.OPERATOR_PWD + "',");
                sb.Append(_operator.RULE_ID);
                sb.Append(")");
                DataBase.SqlCommand(sb.ToString());
                sb.Clear();
                flag = true;
            }
            catch (Exception ex)
            {
                flag = false;
            }
            return flag;
        }

        public bool IsExistAboutOperatorID(string id)
        {
            int count = -1;
            bool flag = false;
            OracleDataReader dataReader;
            string sqlText = "select count(*) from operator where operator_id = '" + id + "'";
            dataReader = DataBase.SqlReader(sqlText);
            while (dataReader.Read())
            {
                count = (int)dataReader.GetOracleNumber(0);
            }
            if (count == 0)
            {
                flag = false;
            }
            else
            {
                flag = true;
            }
            dataReader.Close();
            dataReader.Dispose();
            return flag;
        }

        public OPERATOR GetOperatorByID(string id)
        {
            OPERATOR optr = new OPERATOR();
            Rules rules = new Rules();
            OracleDataReader dataReader;
            try
            {
                string sqlText = "select USR_ID,USR_NAME,USR_PWD from tbl_metro_user_info where USR_ID = '" + id + "'";
                dataReader = DataBase.SqlReader(sqlText);
                while (dataReader.Read())
                {
                    optr.OPERATOR_ID = dataReader.GetOracleString(0).ToString();
                    optr.OPERATOR_NAME = dataReader.GetOracleString(1).ToString();
                    optr.OPERATOR_PWD = dataReader.GetOracleString(2).ToString();
                    //optr.RULE_ID = (int)dataReader.GetOracleNumber(3);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            catch (Exception ex)
            {
                return null;
            }
            return optr;
        }
    }
}
